// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Titanium.UI.setBackgroundColor('#000');

//ネットワークチェック
if(Titanium.Network.online == false){
	alert('オフラインなのでデータ取得できません');
}else{
	//インジケーター生成
	var actInd = Titanium.UI.createActivityIndicator({
					bottom:10,height:50,width:10
				});
	actInd.message = 'Loading....';
	//インジケーター表示
	actInd.show();
	
	//取得XML　URL
	var xmlUrl = 'http://gridappweb.appspot.com/user2.xml';

	//HTTPオブジェクト作成
	var xhr = Titanium.Network.createHTTPClient();
	xhr.open("GET",xmlUrl,true);

	//xmlを読み込んだ処理
	xhr.onload = function(){
		//XML取得
		var xmlDocument = this.responseXML.documentElement;
		
		
		//TabGroup生成
		var tabGroup = Titanium.UI.createTabGroup();
		
		/** プロフィールタブ **/
		//<user>ノードを取得
		var userProfile = xmlDocument.getElementsByTagName('user').item(0);
		var photoList = userProfile.getElementsByTagName('photo');
		var name = userProfile.getElementsByTagName('name').item(0).text;
		var name_ja = userProfile.getElementsByTagName('name_ja').item(0).text;
		var tel = userProfile.getElementsByTagName('tel').item(0).text;
		var mail = userProfile.getElementsByTagName('mail').item(0).text;
		var belong = userProfile.getElementsByTagName('belong').item(0).text;
		
		//<infolist>ノードを取得
		var infoGroup = xmlDocument.getElementsByTagName('infolist').item(0);
		var infoList = infoGroup.getElementsByTagName('info');
		
		//<color>ノードを取得
		var colorGroup = xmlDocument.getElementsByTagName('color').item(0);
		var profile_back = colorGroup.getElementsByTagName('profile_back').item(0).text;
		var profile_text = colorGroup.getElementsByTagName('profile_text').item(0).text;
		var info_back = colorGroup.getElementsByTagName('info_back').item(0).text;
		var info_text = colorGroup.getElementsByTagName('info_text').item(0).text;
		var blog_back = colorGroup.getElementsByTagName('blog_back').item(0).text;
		var blog_text = colorGroup.getElementsByTagName('blog_text').item(0).text;
		var twitter_back = colorGroup.getElementsByTagName('twitter_back').item(0).text;
		var twitter_text = colorGroup.getElementsByTagName('twitter_text').item(0).text;
		
		
		
		
		
		//タグを生成
		var win = Titanium.UI.createWindow({  
	    	title: 'Profile',
	    	backgroundColor:profile_back,
	    	url:'profileTab.js'
		});
		var tab = Titanium.UI.createTab({  
	    	title: 'Profile',
	    	icon:'images/tabIcon/games.png',
	    	backgroundColor:'#000',
	    	window:win
		});
		
		//profileTab.jsへの値受け渡し
		//profile
		win.photoList = photoList;
		win.name = name;
		win.name_ja = name_ja;
		win.tel = tel;
		win.mail = mail;
		win.belong = belong;
		//info
		win.infoList = infoList;
		//color
		win.profile_back = profile_back;
		win.profile_text = profile_text;
		win.info_back = info_back;
		win.info_text = info_text;
		
		//tab登録
		tabGroup.addTab(tab);
				
		/** 各種ツール **/
		//<tab>ノードを取得
		var tabList = xmlDocument.getElementsByTagName('tab');
		
		for(var i = 0;i < tabList.length;i++){
			var tab = tabList.item(i);
			//<title>
			var tabTitle = tab.getElementsByTagName('title').item(0);
			tabTitle = tabTitle.text;
			//<url>
			var viewUrl = tab.getElementsByTagName('url').item(0);
			viewUrl = viewUrl.text;
			
			if(tabTitle != null && viewUrl != null && tabTitle != '' && viewUrl != ''){
				switch (tabTitle){
					//twitter
					case 'twitter':
						//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					url:'tweetTab.js'
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					icon:'images/tabIcon/clock.png',
	    					backgroundColor:'#000',
	    					window:win
						});
						//URLをセット
						win.tweetUrl = viewUrl;
						//tab登録
						tabGroup.addTab(tab);
						break;
						
					//facebook
				  	case 'facebook':
				  		//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					window:win
						});
						tabGroup.addTab(tab);
						break;
						
					//blog
					case 'blog':
						//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					url:'blogTab.js'
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					icon:'images/tabIcon/pencilangled.png',
	    					backgroundColor:'#000',
	    					window:win
						});
						//URLをセット
						win.blogUrl = viewUrl;
						win.tab = tab;
						//tab登録
						tabGroup.addTab(tab);
						break;
					
					//mixi
					case 'mixi':
						//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					window:win
						});
						tabGroup.addTab(tab);
						break;
					
					//HP
					case 'HP':
						//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					url:'hpTab.js'
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					icon:'images/tabIcon/house.png',
	    					backgroundColor:'#000',
	    					window:win
						});
						//URLをセット
						win.hpUrl = viewUrl;
						tabGroup.addTab(tab);
						break;
					
					//other
					case 'other':
						//タグを生成
						var win = Titanium.UI.createWindow({  
	    					title: tabTitle,
	    					backgroundColor:'#000',
	    					url:'hpTab.js'
						});
						var tab = Titanium.UI.createTab({  
	    					title: tabTitle,
	    					icon:'images/tabIcon/star.png',
	    					backgroundColor:'#000',
	    					window:win
						});
						//URLをセット
						win.hpUrl = viewUrl;
						tabGroup.addTab(tab);
						break;
				}
				
			}
			 
		}
		//タブグループを開く
		tabGroup.open();
		//インジケーター非表示
		actInd.hide();
	}
	//リクエスト送信
	xhr.send();
}
